package com.dmwork.service.interaction.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.common.base.result.R;
import com.dmwork.service.interaction.entity.Feed;
import com.dmwork.service.interaction.entity.FeedPictureInfo;
import com.dmwork.service.interaction.entity.vo.FeedInfoVo;
import com.dmwork.service.interaction.feign.CustomerFeignService;
import com.dmwork.service.interaction.service.FeedPictureInfoService;
import com.dmwork.service.interaction.service.FeedService;
import com.vdurmont.emoji.EmojiParser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Transactional
@RestController
@RequestMapping("/admin/dm_work/interaction/feed")
public class AdminFeedController {

    @Autowired
    private FeedService feedService;
    @Autowired
    private CustomerFeignService customerFeignService;
    @Autowired
    private FeedPictureInfoService feedPictureInfoService;
    @Value("${custom.feed.picture-count}")
    private Integer feedPictureCount;

    @ApiOperation("获取所有用户的动态列表")
    @GetMapping("get_feed_list/{page}/{limit}")
    public R getFeedList(@PathVariable Integer page, @PathVariable Integer limit) {
        //按照最新时间获取获取动态信息分页
        Page<Feed> feedPage = new Page<>(page, limit);
        QueryWrapper<Feed> queryWrapper = new QueryWrapper<Feed>().orderByDesc("gmt_create");
        feedPage = feedService.page(feedPage, queryWrapper);
        if (feedPage.getTotal() == 0) {
            return R.ok().message("暂无数据").data("total", 0);
        }

        //获取这些动态所属的用户的用户名和头像，以及动态的图片
        List<String> feedIdList = new ArrayList<>(limit);
        Set<String> customerIdSet = new HashSet<>(limit);
        for (Feed feed : feedPage.getRecords()) {
            feedIdList.add(feed.getId());
            customerIdSet.add(feed.getCustomerId());
        }
        List<String> customerIdList = new ArrayList<>(customerIdSet.size());
        for (String s : customerIdSet) {
            customerIdList.add(s);
        }

        List<FeedPictureInfo> feedPictureInfoList = feedPictureInfoService.list(new QueryWrapper<FeedPictureInfo>().in("feed_id", feedIdList));

        R customerBriefInfoListResult = customerFeignService.getBriefInfoList(customerIdList);
        if (!customerBriefInfoListResult.getSuccess()) {
            return R.error().message("获取失败");
        }
        Map<String, List<String>> customerBriefInfoResultMap = (Map<String, List<String>>) customerBriefInfoListResult.getData().get("result");


        //数据组装
        List<FeedInfoVo> feedInfoVoList = new ArrayList<>();
        for (Feed feed : feedPage.getRecords()) {
            FeedInfoVo feedInfoVo = new FeedInfoVo();
            //转换Emoji
            if (!StringUtils.isEmpty(feed.getContent())) {
                feed.setContent(EmojiParser.parseToUnicode(feed.getContent()));
            }
            BeanUtils.copyProperties(feed, feedInfoVo);
            //设置该动态所属用户的用户名和头像
            feedInfoVo.setCustomerName(customerBriefInfoResultMap.get(feed.getCustomerId()).get(0));
            feedInfoVo.setAvatar(customerBriefInfoResultMap.get(feed.getCustomerId()).get(1));
            if (feed.getPictureCount() > 0) {
                List<String> pictureUrlList = new ArrayList<>();
                int count = 0; //对图片url计数。业务要求，每个动态的图片数不能超过指定值，所以每找到一张图片的url，就count+1，当count>指定值时，就停止遍历
                for (FeedPictureInfo feedPictureInfo : feedPictureInfoList) {
                    if (count < feedPictureCount && feedPictureInfo.getFeedId().equals(feed.getId())) {
                        pictureUrlList.add(feedPictureInfo.getPictureUrl());
                        count++;
                    } else if (count >= feedPictureCount) {
                        break;
                    }
                }
                feedInfoVo.setPictureUrlList(pictureUrlList);
            }
            //添加已组装好数据的动态
            feedInfoVoList.add(feedInfoVo);
        }
        return R.ok().data("total", feedPage.getTotal()).data("rows", feedInfoVoList);
    }

}
